#!/bin/sh

# 检测 JVM 类型
JAVA_VERSION_OUTPUT=$(java -version 2>&1)
if echo "$JAVA_VERSION_OUTPUT" | grep -q "OpenJ9"; then
    JVM_TYPE="OpenJ9"
else
    JVM_TYPE="HotSpot"
fi

echo "Detected JVM: $JVM_TYPE"

# 公共配置
JAVA_OPTS="-Dfile.encoding=UTF-8"
JAVA_OPTS="$JAVA_OPTS -Duser.timezone=Asia/Shanghai"
JAVA_OPTS="$JAVA_OPTS -Djava.awt.headless=true"
JAVA_OPTS="$JAVA_OPTS -Dlog4j2.formatMsgNoLookups=true"

# 内存配置（通用）
JAVA_OPTS="$JAVA_OPTS -Xms512m -Xmx2048m"

if [ "$JVM_TYPE" = "OpenJ9" ]; then
    echo "Applying OpenJ9-specific JVM options..."
    JAVA_OPTS="$JAVA_OPTS -Xscmx50m"
    JAVA_OPTS="$JAVA_OPTS -Xshareclasses:name=appcache,cacheDir=/tmp/javasharedcache"
    JAVA_OPTS="$JAVA_OPTS -Xquickstart"
    # GC 策略
    JAVA_OPTS="$JAVA_OPTS -Xnoclassgc"
    JAVA_OPTS="$JAVA_OPTS -Xgcpolicy:balanced" # balanced gencon
    # 虚拟化调优
    JAVA_OPTS="$JAVA_OPTS -Xtune:virtualized"
    # 禁止 System.gc() 显示调用，防止频繁Full GC
    JAVA_OPTS="$JAVA_OPTS -Xdisableexplicitgc"
    # JIT 参数，启用内联，关闭异步编译
    # 调优 JIT 编译阈值和策略
    JAVA_OPTS="$JAVA_OPTS -Xjit:count=1000,disableAsyncCompilation"
    # 线程栈大小（根据应用需要调节）
    JAVA_OPTS="$JAVA_OPTS -Xss512k"  # 默认512KB线程栈
    
else
    echo "Applying HotSpot/OpenJDK-specific JVM options..."
    JAVA_OPTS="$JAVA_OPTS -XX:MetaspaceSize=512M -XX:MaxMetaspaceSize=512M"
    JAVA_OPTS="$JAVA_OPTS -XX:CICompilerCount=8"
    JAVA_OPTS="$JAVA_OPTS -XX:+UseCompressedOops"
    JAVA_OPTS="$JAVA_OPTS -XX:SurvivorRatio=4 -XX:TargetSurvivorRatio=90"
    JAVA_OPTS="$JAVA_OPTS -XX:ReservedCodeCacheSize=128m"
    JAVA_OPTS="$JAVA_OPTS -XX:-UseAdaptiveSizePolicy"
    JAVA_OPTS="$JAVA_OPTS -XX:-DontCompileHugeMethods"
    JAVA_OPTS="$JAVA_OPTS -Xss512k"
    JAVA_OPTS="$JAVA_OPTS -XX:MaxTenuringThreshold=15"
    JAVA_OPTS="$JAVA_OPTS -XX:+UseGCOverheadLimit"
    JAVA_OPTS="$JAVA_OPTS -XX:AllocatePrefetchDistance=256 -XX:AllocatePrefetchStyle=1"
    JAVA_OPTS="$JAVA_OPTS -XX:+UseG1GC -XX:+UseStringDeduplication"
    JAVA_OPTS="$JAVA_OPTS -XX:InitiatingHeapOccupancyPercent=50"
    JAVA_OPTS="$JAVA_OPTS -XX:+UnlockExperimentalVMOptions"
    JAVA_OPTS="$JAVA_OPTS -XX:MaxGCPauseMillis=200 -XX:G1HeapWastePercent=10"
fi

#JAVA_OPTS="$JAVA_OPTS -Djava.awt.headless=true -Dsun.java2d.fontpath=/usr/share/fonts"

# 应用可选参数
JAVA_OPTS="$JAVA_OPTS -Ddruid.mysql.usePingMethod=false -Ddruid.registerToSysProperty=true"

# 启动应用
echo "Starting Java application with options:"
echo "$JAVA_OPTS"
#输出到当前目录下的nohup.out
# nohup java $JAVA_OPTS -jar /opt/app-store/jar/jx-fota-android.jar >> /opt/logs/jx-fota-android.log.out 2>&1 &
# Start app
APP_JAR="${APP_JAR:-jx-fota-android.jar}"
APP_JAR_PATH="/opt/app-store/jar/$APP_JAR"

if [ ! -f "$APP_JAR_PATH" ]; then
  echo "Error: JAR not found at $APP_JAR_PATH"
  exit 1
fi

echo "Starting Java app with options:"
echo "$JAVA_OPTS"

# Recommended: use exec for PID 1 handover to Java
exec java $JAVA_OPTS -jar "$APP_JAR_PATH"

#如果需要输出到docker logs可以用，但是会需要注意，这个命令会一直运行
#tail -f /opt/logs/sentinel-dashboard-1.8.2.log.out
#不输出日志，保持运行状态
# tail -F /opt/logs/null

#该模式无法使用exec "$@",因为做错加入了start
#exec "$@"
